package Dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Dao.Daogouwuche;
import entity.gouwuche;
import utils.BaseDao;

public class Daoimplgouwuche extends BaseDao implements Daogouwuche {

	@Override
	public boolean add(gouwuche g) {
		String sql = "insert into gouwuche value(null,?,?,?,?,?,1)";
		Object[] object = {g.getTu(),g.getMing(),g.getZhi(),g.getUid(),g.getMoney()};
		return super.executeUpdate(sql, object);
	}

	@Override
	public gouwuche sel(gouwuche g) {
		String sql = "select * from gouwuche where uid=? and name =?";
		Object[] object ={g.getUid(),g.getMing()};
		ResultSet set = super.executeQuery(sql, object);
		gouwuche gou = null;
		try {
			while(set.next()){
				gou = new gouwuche();
				gou.setId(set.getInt(1));
				gou.setTu(set.getString(2));
				gou.setMing(set.getString(3));
				gou.setZhi(set.getString(4));
				gou.setUid(set.getInt(5));
				gou.setMoney(set.getDouble(6));
				gou.setSum(set.getInt(7));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}  
		return gou;
	}

	@Override
	public boolean upd(gouwuche g) {
		String sql = "update `xiangmu`.`gouwuche` set `sum` = ? where `uid` = ? and name = ? ";
		Object[] object = {g.getSum(),g.getUid(),g.getMing()};
		return super.executeUpdate(sql, object);
	}

	@Override
	public int sel(int id) {
		String  sql = "select count(*) from gouwuche where uid = ?";
		Object[] object = {id};
		ResultSet set = super.executeQuery(sql, object);
		int ran = -1;
		try {
			while(set.next()){
				ran = set.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ran;
	}

	@Override
	public List<gouwuche> selall(int id) {
		String sql = "select * from gouwuche where uid = ?";
		Object[] object ={id};
		ResultSet set = super.executeQuery(sql, object);
		List<gouwuche> list = new ArrayList<gouwuche>();
		try {
			while(set.next()){
				gouwuche gou = new gouwuche();
				gou.setId(set.getInt(1));
				gou.setTu(set.getString(2));
				gou.setMing(set.getString(3));
				gou.setZhi(set.getString(4));
				gou.setUid(set.getInt(5));
				gou.setMoney(set.getDouble(6));
				gou.setSum(set.getInt(7));
				list.add(gou);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}  
		return list;
	}

	@Override
	public boolean jiaupd(int uid, String name) {
		String sql = "update `xiangmu`.`gouwuche` set `sum` = `sum`+1 where `uid` = ? and name = ?";
		Object[] object = {uid,name};
		return super.executeUpdate(sql, object);
	}

	@Override
	public boolean jianupd(int uid, String name) {
		String sql = "update `xiangmu`.`gouwuche` set `sum` = `sum`-1 where `uid` = ? and name = ?";
		Object[] object = {uid,name};
		return super.executeUpdate(sql, object);
	}

	@Override
	public boolean shangdel(int uid, String name) {
		String sql = "delete from `xiangmu`.`gouwuche` where `uid` = ? and name = ?";
		Object[] object = {uid,name};
		return super.executeUpdate(sql, object);
	}

	@Override
	public List<gouwuche> tianxie(String[] s) {
		List<gouwuche> list = new ArrayList<gouwuche>();
		for (int i = 0; i < s.length; i++) {
			String sql = "select * from gouwuche where id = ?";
			Object[] object ={s[i]};
			ResultSet set = super.executeQuery(sql, object);
			try {
				while(set.next()){
					gouwuche gou = new gouwuche();
					gou.setId(set.getInt(1));
					gou.setTu(set.getString(2));
					gou.setMing(set.getString(3));
					gou.setZhi(set.getString(4));
					gou.setUid(set.getInt(5));
					gou.setMoney(set.getDouble(6));
					gou.setSum(set.getInt(7));
					list.add(gou);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}

	@Override
	public boolean updqingchu(String[] s) {
		int j = 0;
		for (int i = 0; i < s.length; i++) {
			String sql = "update `xiangmu`.`gouwuche` set `uid` = '-1' where `id` = ?";
			Object[] object ={s[i]};
			boolean set = super.executeUpdate(sql, object);
			if(set){
				j++;
			}
			
		}
		return j==s.length;
	}

	@Override
	public int zhongjia(String[] s) {
		int jia = 0;
		for (int i = 0; i < s.length; i++) {
			String sql = "select * from gouwuche where id = ?";
			Object[] object ={s[i]};
			ResultSet set = super.executeQuery(sql, object);
			try {
				while(set.next()){
					gouwuche gou = new gouwuche();
					gou.setId(set.getInt(1));
					gou.setTu(set.getString(2));
					gou.setMing(set.getString(3));
					gou.setZhi(set.getString(4));
					gou.setUid(set.getInt(5));
					gou.setMoney(set.getDouble(6));
					gou.setSum(set.getInt(7));
					jia+=gou.getMoney();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return jia;
	}

}
